package com.microcdyUser.controller;
import com.microcdy.sys.pojo.SysPost;
import com.microcdyUser.service.SysPostService;
import com.github.pagehelper.PageInfo;
import entity.Result;
import entity.StatusCode;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/****
 * @Author:cdy
 * @Description:
 * @Date 2019/6/14 0:18
 *****/

@RestController
@RequestMapping("/sysPost")
@CrossOrigin
public class SysPostController {

    @Autowired
    private SysPostService sysPostService;

    /***
     * SysPost分页条件搜索实现
     * @param sysPost
     * @param page
     * @param size
     * @return
     */
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@RequestBody(required = false)  SysPost sysPost, @PathVariable  int page, @PathVariable  int size){
        //调用SysPostService实现分页条件查询SysPost
        PageInfo<SysPost> pageInfo = sysPostService.findPage(sysPost, page, size);
        return new Result(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * SysPost分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@PathVariable  int page, @PathVariable  int size){
        //调用SysPostService实现分页查询SysPost
        PageInfo<SysPost> pageInfo = sysPostService.findPage(page, size);
        return new Result<PageInfo>(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * 多条件搜索品牌数据
     * @param sysPost
     * @return
     */
    @PostMapping(value = "/search" )
    public Result<List<SysPost>> findList(@RequestBody(required = false)  SysPost sysPost){
        //调用SysPostService实现条件查询SysPost
        List<SysPost> list = sysPostService.findList(sysPost);
        return new Result<List<SysPost>>(true,StatusCode.OK,"查询成功",list);
    }

    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable Long id){
        //调用SysPostService实现根据主键删除
        sysPostService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /***
     * 修改SysPost数据
     * @param sysPost
     * @param id
     * @return
     */
    @PutMapping(value="/{id}")
    public Result update(@RequestBody  SysPost sysPost,@PathVariable Long id){
        //设置主键值
        sysPost.setPostId(id);
        //调用SysPostService实现修改SysPost
        sysPostService.update(sysPost);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /***
     * 新增SysPost数据
     * @param sysPost
     * @return
     */
    @PostMapping
    public Result add(@RequestBody   SysPost sysPost){
        //调用SysPostService实现添加SysPost
        sysPostService.add(sysPost);
        return new Result(true,StatusCode.OK,"添加成功");
    }

    /***
     * 根据ID查询SysPost数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<SysPost> findById(@PathVariable Long id){
        //调用SysPostService实现根据主键查询SysPost
        SysPost sysPost = sysPostService.findById(id);
        return new Result<SysPost>(true,StatusCode.OK,"查询成功",sysPost);
    }

    /***
     * 查询SysPost全部数据
     * @return
     */
    @GetMapping
    public Result<List<SysPost>> findAll(){
        //调用SysPostService实现查询所有SysPost
        List<SysPost> list = sysPostService.findAll();
        return new Result<List<SysPost>>(true, StatusCode.OK,"查询成功",list) ;
    }
}
